package com.liu.qqRobot.climbingTower.handler;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.climbingTower.entity.base.ClimbingTowerUserInfo;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerUserEquipService;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerUserInfoService;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.climbingTower.entity.dto.ClimbingTowerUserEquipDTO;
import com.liu.qqRobot.climbingTower.entity.dto.UserEquipAttributeDTO;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class ClimbingTowerMyInfoHandler {

    @Resource
    private ClimbingTowerUserInfoService climbingTowerUserInfoService;

    @Resource
    private ClimbingTowerUserEquipService climbingTowerUserEquipService;

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        QueryWrapper<ClimbingTowerUserInfo> climbingTowerUserInfoQueryWrapper = new QueryWrapper<>();
        climbingTowerUserInfoQueryWrapper.eq("group_id", groupId);
        climbingTowerUserInfoQueryWrapper.eq("user_id", userId);
        ClimbingTowerUserInfo climbingTowerUserInfo = climbingTowerUserInfoService.getOne(climbingTowerUserInfoQueryWrapper);
        ClimbingTowerUserEquipDTO climbingTowerUserEquipDTO = climbingTowerUserEquipService.getUserEquip(groupId, userId);
        UserEquipAttributeDTO userEquipAttributeDTO = climbingTowerUserEquipService.calcUserEquipAttribute(groupId, userId);
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("climbingTowerUserInfo", climbingTowerUserInfo);
        templateParam.put("climbingTowerUserEquip", climbingTowerUserEquipDTO);
        templateParam.put("userEquipAttributeDTO", userEquipAttributeDTO);
        try {
            File file = HtmlToImageUtils.htmlToImage("climbingTowerMyInfoTemplate.html", templateParam,
                    "src/main/resources/picture/climbingTowerMyInfo.png", 2500);
            String encode = Base64.encode(file);
            String msg = "[CQ:at,qq=" + userId + "]\n[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
            QQClientUtils.sendMsg(msg, userId, groupId, isGroup);
            qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.CLIMBING_TOWER_MY_INFO);
        } catch (IOException | ParserConfigurationException | SAXException | TemplateException e) {
            log.error("获取爬塔我的信息展示模板失败:{}", e.getMessage(), e);
        }
    }
}
